package logic

import (
	"context"
	"houseRpc/models"

	"houseRpc/house"
	"houseRpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserListLogic {
	return &UserListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UserListLogic) UserList(in *house.UserListRequest) (*house.UserListResponse, error) {
	// todo: add your logic here and delete this line
	var userses []models.UserRealName
	var userList []*house.ListUsers
	query := models.DB.Model(models.RealName{})
	if in.Status == 0 {
		query = query.Where(&models.User{Status: 1})
	}
	if in.Status != 0 {
		query = query.Where("users.status=?", in.Status)
	}
	if in.Keyword != "" {
		query = query.Where("mobile LIKE ?", "%"+in.Keyword+"%")
	}

	query = query.Select("users.*,real_names.real_name,real_names.id_card").
		Joins("JOIN users on real_names.user_id=users.id").
		Find(&userses)

	for _, user := range userses {
		list := house.ListUsers{
			Mobile:   user.Mobile,
			RealName: user.RealName,
			IdCard:   user.IdCard,
			Status:   user.Status,
		}
		userList = append(userList, &list)
	}
	return &house.UserListResponse{
		List: userList,
	}, nil
}
